package com.atguigu.gmall.all.controller;

import com.atguigu.gmall.activity.client.ActivityFeignClient;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.activity.SeckillGoods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author atguigu-mqx
 */
@Controller
public class SeckillController {

    @Autowired
    private ActivityFeignClient activityFeignClient;

    //  http://activity.gmall.com/seckill.html
    @GetMapping("seckill.html")
    public String getSeckillList(HttpServletRequest request){
        //  需要后台存储一个list
        List<SeckillGoods> seckillGoodsList = activityFeignClient.findAll();
        request.setAttribute("list",seckillGoodsList);
        return "seckill/index";
    }

    // 商品详情 th:href="'/seckill/'+${item.skuId}+'.html'"
    @GetMapping("seckill/{skuId}.html")
    public String seckillItem(@PathVariable Long skuId,HttpServletRequest request){
        //  获取秒杀商品对象
        SeckillGoods seckillGoods = activityFeignClient.getSeckillGoods(skuId);
        //  后台需要存储一个item
        request.setAttribute("item",seckillGoods);
        return "seckill/item";
    }

    //  http://activity.gmall.com/seckill/queue.html?skuId=24&skuIdStr=c81e728d9d4c2f636f067f89cc14862c
    @GetMapping("seckill/queue.html")
    public String queue(HttpServletRequest request){
        //  ${skuId} ${skuIdStr}
        request.setAttribute("skuId",request.getParameter("skuId"));
        request.setAttribute("skuIdStr",request.getParameter("skuIdStr"));
        //  返回排队页面！
        return "seckill/queue";
    }

    //  <a href="/seckill/trade.html" target="_blank">去下单</a>
    @GetMapping("/seckill/trade.html")
    public String seckillTrade(Model model){
        // userAddressList  detailArrayList totalNum totalAmount
        Result<Map> result = activityFeignClient.trade();
        //  保存数据：
        model.addAllAttributes(result.getData());
        if (result.isOk()){
            //  下单页面！ 与订单模块的页面一样！
            return "seckill/trade";
        }else {
            model.addAttribute("message","下单失败....");
            return "seckill/fail";
        }

    }

}
